package com.sdkx.app.fragment;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.sdkx.app.R;
import com.sdkx.app.activitys.MainActivity;
import com.sdkx.app.dialog.AirSpaceDialog;
import com.sdkx.app.dialog.AirSpaceOption;
import com.sdkx.app.datalayers.AirSpaceLayer;
import com.sdkx.app.datalayers.LayersFactory;
import com.sdkx.app.model.AirSpace;
import com.sdkx.app.model.AirSpaceGeneral;
import com.sdkx.app.model.AirSpacePoint;

import org.litepal.LitePal;

import java.util.ArrayList;
import java.util.List;

public class AirspaceFragment  extends BaseFragment implements View.OnClickListener{
    AirSpaceGeneral general;
    TextView title;
    RecyclerView recyclerView;
    AirspaceAdapter airspaceAdapter;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_menu_airspace1,null);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        Bundle bundle = getArguments();
        if(bundle == null) {
            goBack();
            return;
        }
        int index = bundle.getInt("AIRSPACE_ITEM");
        List<AirSpaceGeneral>list = LitePal.where("id="+index).find(AirSpaceGeneral.class);
        if(list.size() ==0) goBack();
        general = list.get(0);
        title =  view.findViewById(R.id.title_name);
        title.setText(general.getName());
        recyclerView = view.findViewById(R.id.recyclerView);
        Button add = view.findViewById(R.id.add);
        add.setOnClickListener(this);
        add.setVisibility(View.VISIBLE);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        airspaceAdapter = new AirspaceAdapter();
        recyclerView.setAdapter(airspaceAdapter);
        //返回上一级点击事件
        Button tv_all_air_spaces = (Button) view.findViewById(R.id.tv_all_air_spaces2);

        //返回上一级按钮
        tv_all_air_spaces.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                goBack();
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.add:
               final AirSpaceOption optiondlg = new AirSpaceOption(getContext());
                optiondlg.setiOptionCallback(new AirSpaceOption.IOptionCallback() {
                    @Override
                    public void onCallback(int option) {
                        AirSpaceDialog dlg = new AirSpaceDialog(getContext(),option);
                        dlg.setIasCallback(new AirSpaceDialog.IASCallback() {
                            @Override
                            public void onSave(AirSpace airSapce) {
                                general.getSpacelist().add(airSapce);
                                general.save();
                                airspaceAdapter.setList(general.getSpacelist());
                                airspaceAdapter.notifyDataSetChanged();
                                refreshMap();
                            }
                        });
                        dlg.show();
                        optiondlg.dismiss();
                    }
                });
                optiondlg.show();
                break;
        }
    }

    public class AirspaceAdapter extends RecyclerView.Adapter<AirspaceAdapter.ViewHolder>{
        List<AirSpace> list  = new ArrayList<>();
        public AirspaceAdapter(){
            list =  general.getSpacelist();
        }
        public List<AirSpace> getList() {
            return list;
        }
        public void setList(List<AirSpace> list){
            this.list = list;
        }
        @NonNull
        @Override
        public AirspaceAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            return new ViewHolder(getLayoutInflater().inflate(R.layout.airspace_item,null));
        }

        @Override
        public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
            holder.init(list.get(position));
        }

        @Override
        public int getItemCount() {
            return list.size();
        }

        public class ViewHolder extends RecyclerView.ViewHolder{
            CheckBox checkBox;
            TextView textView;
            ImageView next;
            AirSpace airSpace;
            public ViewHolder(View itemView) {
                super(itemView);
                itemView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,60));
                checkBox = itemView.findViewById(R.id.checkid);
                textView = itemView.findViewById(R.id.name);
                next = itemView.findViewById(R.id.next);
                next.setVisibility(View.GONE);
                checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        airSpace.setVisible(isChecked);
                        airSpace.update(airSpace.getId());
                    }
                });
                itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(final View item) {
                        final View popview = getLayoutInflater().inflate(R.layout.marklist_menu_layout, null);
                        final PopupWindow popupWindow = new PopupWindow(popview,
                                200, LinearLayout.LayoutParams.WRAP_CONTENT);

                        popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
                        popupWindow.setFocusable(true);
                        popupWindow.setOutsideTouchable(true);
                        popupWindow.showAsDropDown(item, item.getWidth() - 20, -(item.getHeight()), Gravity.LEFT | Gravity.TOP);
                        popview.findViewById(R.id.modify).setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                AirSpaceDialog dialog = new AirSpaceDialog(getContext(),airSpace);
                                dialog.setIasCallback(new AirSpaceDialog.IASCallback() {
                                    @Override
                                    public void onSave(AirSpace airSapce) {
                                        general.getSpacelist().add(airSapce);
                                        general.save();
                                        airspaceAdapter.setList(general.getSpacelist());
                                        airspaceAdapter.notifyDataSetChanged();
                                        refreshMap();
                                    }
                                });
                                dialog.show();
                                popupWindow.dismiss();
                            }
                        });
                        popview.findViewById(R.id.delete).setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                general.getSpacelist().remove(airSpace);
                                list.remove(airSpace);
                                airSpace.delete();
                                airspaceAdapter.notifyDataSetChanged();
                                refreshMap();
                                popupWindow.dismiss();
                            }
                        });
                        popview.findViewById(R.id.map).setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                               List<AirSpacePoint> points =  airSpace.getPoints();
                               if(points.size() == 0)
                                   points = airSpace.getPointsIndb();
                               if(points.size() > 0) {
                                   AirSpacePoint point = points.get(0);
                                   ((MainActivity) getActivity()).getMapControl().slideAnimation(point.getDoubleLongitude(),point.getDoubleLatitude());
                               }
                            }
                        });
                    }
                });
            }
            public void init(AirSpace general){
                airSpace = general;
                textView.setText(general.getName());
            }
        }
    }
    void refreshMap(){
        LayersFactory.getInstance().updateSingleCustom(AirSpaceLayer.class, new LayersFactory.IUpdateLister() {
            @Override
            public void onUpdated() {
                ((MainActivity) getActivity()).getMapControl().refresh();
            }
        });
    }
}
